<?	/*
	// File:	linux-user.wcp.phps
	// Purpose:	linux user manipulation
	// Author:	Felix <webmaster@can-host.com>
	*/

// This has been moved to unix-users.mod.phps
/*
// Handle user manipulation
if ($type == "user") {
	// Create User :: expect *
	if ($data['action'] == "create") {
		
		// Check for existing user
		$userexists = user_exists($data['username']);
		
		// check user root, it must exist or if contains %USER%, create new dir.
		if (!is_dir($data['root'])) {
			$tmp = explode("/",$data['root']);
			array_pop($tmp);
			$tmp = implode("/",$tmp);
			if (is_dir($tmp)) {
				mkdir($data['root'],0775);
				chmod($data['root'],0775);
//				chown($data['root'],$data['username']);
				chgrp($data['root'],"g".$data['id']);
				clearstatcache();
			}
		} else {
			chmod($data['root'],0775);
			chgrp($data['root'],"g".$data['id']);
			clearstatcache();
		}
		
		if (is_dir($data['root']) && !$userexists) {
			// Set appropriate shell
			if ($data['shell'] == "on")
				$shell = $cfg['prog']['shell'];
			else
				$shell = $cfg['prog']['noshell'];
				
			// create user
			if ($data['type'] == "standard") {
				
				$data['name'] = addslashes($data['name']);

				if ($cfg['os'] == 'linux') {
					//see if MD5 passwords are enabled, otherwise use DES (checked on redhat 7.1 - 9.0)
	 				if (is_file($cfg['authconfig']) AND strpos(implode('', file($cfg['authconfig'])), 'USEMD5=yes') !== false)
	 					$crypt_password = crypt($data['password'], '$1$'.make_salt().'$');
	 				else
	 					$crypt_password = crypt($data['password'], $cfg['key']);
					exec($cfg['prog']['uadd']." -g g$data[id] -d '$data[root]' -s $shell -c '$data[name]' -p '$crypt_password' $data[username]");
					
						$hdquota = intval($data['hd'] * 1024);
						if ($data['hd'] == -1) { $hdquota = 0; }
						switch ($cfg['osversion']) {
							case 'RedHat6.2':
								$setq = $cfg['prog']['squota']." -u $data[username] $cfg[devhd] $hdquota $hdquota 0 0";
								break;
							default:
								$setq = $cfg['prog']['squota']." -u $data[username] $hdquota $hdquota 0 0 $cfg[devhd]";
						}
						exec($setq);
				}
				elseif ($cfg['os'] == 'freebsd') {
					/*
						To add a user on FreeBSD:
						echo "password" | pw adduser user -g group \
							-s shell -d /home/user -c comment -h -

						adds the user 'user' with primary group 'group',
						shell 'shell', home dir '/home/user' with a comment 'comment'

						This is pretty dodgy - the password is listed in ps output...

						To do this from PHP though, we use popen to create a stream to the
						command:
						pw adduser -q -u user -g group \
							-s shell -d /home/user -c comment -h 0

						and then write the password to the file pointer created
						by popen.  This effectively adds the user to the passwd database
						whilst at same time setting the password.

						This saves listing the password in 'ps' listings.
					*/
					// adduser command:
					$pw_cmd = $cfg['prog']['pw']." useradd ".$data["username"]
							." -g g".$data["id"]
							." -s $shell "
							." -d ".$data["root"]
							." -c \"".$data["name"]."\""
							." -h 0";

					// Open a uni-directional stream to the command:
					$fp = popen($pw_cmd, "w");

					// Execute the command, passing the $data["password"] to it:
					fwrite($fp, $data["password"]);

					// Close the pipe:
					fclose($fp);

					/*
						To add a user quota on FreeBSD:
						edquota -e /home:0:99999999 -u user
						adds a soft/hard limit of 0/9999999 kb for the user 'user' on filesystem /home
					*/
						$hdquota = intval($data['hd'] * 1024);
						if ($data['hd'] == -1) { $hdquota = 0; }
						exec($cfg['prog']['squota']." ".$cfg["webdir"].":0:$hdquota -u ".$data["username"]);
				}
			}
		} elseif ($userexists) {
			// log that the user already exists
			webcp_log(1,0,"system","User Creation Error: ".$data['username']." already exists and can not be created.",0);
			mysql_query("DELETE FROM users WHERE username = '".$data['username']."'");
		} elseif (!is_dir($data['root'])) {
			// else log it (race condition??)
			webcp_log(1,0,"system","User Creation Error: Home directory ".$data['root']." does not exist and cannot be created.",0);
		}
		else // else log it (race condition??)
			webcp_log(1,0,"system","User Creation Error: ".$data['root']." does not exist",0);
	}
	
	// Update User :: expect *
	if ($data['action'] == "update") {
		// Set appropriate shell
		if ($data['shell'] == "on")
			$shell = $cfg['prog']['shell'];
		else
			$shell = $cfg['prog']['noshell'];
			
		if ($data['username'] != $data['username1']) {
			if (user_exists($data['username'])) {
				mysql_query("UPDATE users SET username = '".$data['username1']."' WHERE username = '".$data['username']."'");
				webcp_log(1,0,"system","User Rename Error: ".$data['username']." already exists so ".$data['username1']." could not be renamed.",0);
			}
		}
		
		
		// update user
		if ($data['type'] == "standard") {
			$data['name'] = addslashes($data['name']);
			
			if ($cfg['os'] == 'linux') {
				//see if MD5 passwords are enabled, otherwise use DES (checked on redhat 7.1 - 9.0)
				if (is_file($cfg['authconfig']) AND strpos(file_get_contents($cfg['authconfig']), 'USEMD5=yes') !== false)
					$crypt_password = crypt($data['password'], '$1$'.make_salt().'$');
				else
					$crypt_password = crypt($data['password'], $cfg['key']);
				exec($cfg['prog']['umod']." -l '$data[username]' -d '$data[root]' -s $shell -c '$data[name]' -p '$crypt_password' '$data[username1]'");
				$hdquota = intval($data['hd'] * 1024);
				if ($data['hd'] == -1) { $hdquota = 0; }
				switch ($cfg['osversion']) {
					case 'RedHat6.2':
						$setq = $cfg['prog']['squota']." -u '$data[username]' $cfg[devhd] $hdquota $hdquota 0 0";
						break;
					default:
						$setq = $cfg['prog']['squota']." -u '$data[username]' $hdquota $hdquota 0 0 $cfg[devhd]";
				}
				exec($setq);
			}
			elseif ($cfg['os'] == 'freebsd') {
				$pw_cmd = $cfg['prog']['pw']." usermod -q -n ".$data["username1"]." -l ".$data["username"]
						." -g g".$data["id"]
						." -s $shell "
						." -d ".$data["root"]
						." -c \"".$data["name"]."\""
						." -h 0";

				// Open a uni-directional stream to the command:
				$fp=popen($pw_cmd, "w");

				// Execute the command, passing the $data["password"] to it:
				fwrite($fp, $data["password"]);

				// Close the pipe:
				fclose($fp);
				
				// Update quota:
				$hdquota = intval($data['hd'] * 1024);
				if ($data['hd'] == -1) { $hdquota = 0; }
				exec($cfg['prog']['squota']." ".$cfg["webdir"].":0:$hdquota -u ".$data["username"]);
			}
		}
	}
	
	// Suspend User :: expect username
	if ($data['action'] == "suspend") {
		// suspend user
		if ($data['type'] == "standard") {
			if ($cfg['os'] == 'linux')
				exec($cfg['prog']['umod']." -L '".$data['username']."'");
			elseif ($cfg['os'] == 'freebsd')
				exec($cfg['prog']['pw']." lock $data[username]");
		}
	}
	
	// Unsuspend User :: expect username
	if ($data['action'] == "unsuspend") {
		// unsuspend user
		if ($data['type'] == "standard") {
			if ($cfg['os'] == 'linux')
				exec($cfg['prog']['umod']." -U '".$data['username']."'");
			elseif ($cfg['os'] == 'freebsd')
				exec($cfg['prog']['pw']." unlock $data[username]");
		}
	}
	
	// Remove User :: expect username
	if ($data['action'] == "remove") {
		// remove user
		if ($data['type'] == "standard") {
			if ($cfg['os'] == 'linux')
				exec($cfg['prog']['udel']." '".$data['username']."'");
			elseif ($cfg['os'] == 'freebsd')
				exec($cfg['prog']['pw']." userdel -n ".$data['username']);
		}
	}
}
*/
?>
